import ModuleUtil from "@/parent-ui2/src/main/js/utils/ModuleUtil";

export default {
    name: 'RequestUtil',
    axiosInstance: null,
    // entityModuleMap: null,
    install(axiosInstance) {
        this.axiosInstance = axiosInstance
        // this.entityModuleMap = entityModuleMap
        window.$$get = async function (url, params) {
            return (await axiosInstance.get(url, {params})).data
        }
        window.$$post = async function (url, data) {
            return (await axiosInstance.post(url, data || {})).data
        }
    },
    async $$get(url, params) {
        return (await this.axiosInstance.get(url, {params})).data
    },
    async $$post(url, data) {
        return (await this.axiosInstance.post(url, data || {})).data
    },
    async $page(entityName, params) {
        return (await ModuleUtil.loadEntityObjsFun([entityName], null, async (module, entity) =>
            await $$get(`/${module}/commonData/selectPage/${entity}`, params)
        ))[0]
    },
    async $list(entityName, params) {
        return (await ModuleUtil.loadEntityObjsFun([entityName], null, async (module, entity) =>
            await $$get(`/${module}/commonData/selectList/${entity}`, params)
        ))[0];
        // debugger
    },
    async $one(entityName, id) {
        return (await ModuleUtil.loadEntityObjsFun([entityName], null, async (module, entity) =>
            await $$get(`/${module}/commonData/selectOne/${entity}`, {id})
        ))[0]
    },
    async $insert(entityName, data) {
        return (await ModuleUtil.loadEntityObjsFun([entityName], null, async (module, entity) =>
            await $$post(`/${module}/commonData/insert/${entity}`, data)
        ))[0]
    },
    async $saveOrUpdate(entityName, data) {
        return (await ModuleUtil.loadEntityObjsFun([entityName], null, async (module, entity) =>
            await $$post(`/${module}/commonData/insertOrUpdate/${entity}`, data)
        ))[0]
    },
    async $updateById(entityName, data) {
        return (await ModuleUtil.loadEntityObjsFun([entityName], null, async (module, entity) =>
            await $$post(`/${module}/commonData/update/${entity}`, data)
        ))[0]
    },
    async $deleteById(entityName, id) {
        return (await ModuleUtil.loadEntityObjsFun([entityName], null, async (module, entity) =>
            await $$post(`/${module}/commonData/delete/${entity}/${id}`)
        ))[0]
    },
    appendParams(url, params) {
        if (!url.includes('?')) {
            url += '?'
        }
        return url + Object.keys(params)
            .map(key => params[key] && `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`)
            .join('&')
    },
    async simpleGet(url, header = '/api', dataFun = (o) => o.data) {
        try {
            let headers = JSON.parse(process.env.VUE_APP_HEADER||'{}');
            // debugger
            const response = await fetch(header + url,{  headers: headers})
            const data = await response.json()
            return dataFun(data)
        } catch (error) {
            console.error('Error in simpleGet:', error)
            throw error // Rethrow the error for handling by the caller
        }
    },
    async simpleList(entityName, header = '/api', dataFun = (o) => o.data) {
        return (await ModuleUtil.loadEntityObjsFun([entityName], null, async (module, entity) => {
                return (await this.simpleGet(`/${module}/commonData/selectList/${entity}?enabled=1`, header, dataFun));
            }
        ))[0]
    },
}
// export default new ObjectUtil()